import { Reducer, Effect } from 'umi';
import { fetchQuestionList, fetchQuestionAdd} from './service'

export interface StateType {
  list: Array<any>;
}

export interface LoginModelType {
  namespace: string;
  state: StateType;
  effects: {
    fetchQuestionList: Effect,
    fetchQuestionAdd: Effect
  };
  reducers: {
    changeState: Reducer<StateType>;
  };
}

const Model: LoginModelType = {
  namespace: 'questionList',

  state: {
    list: []
  },

  effects: {
    *fetchQuestionList ({payload}, { call, put }) {
      const res = yield call(fetchQuestionList, payload)
      yield put({
        type: 'changeState',
        payload: {
          list: res.data
        }
      })
      console.log(res)
    },
    *fetchQuestionAdd ({payload}, {call, put}) {
      const res = yield call(fetchQuestionAdd, payload)
      console.log(res)
    }
  },

  reducers: {
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload
      };
    },
  },
};

export default Model;
